package util.cleaner;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import puppy.graph.AgeAggregator;
import puppy.graph.DeliciousNode;
import puppy.graph.DeliciousNodeSimple;
import util.io.FileInput;

public class AggregateNodeToNode {
	static private int _id_url = 0;
	static private int _id_tag = 1;
	static private int _url = 2;
	static private int _tag = 3;
	static private int _freq_u = 5;
	static private int _freq_tag = 4;
	static private int _freq_ut = 6;
	static private int _age = 7;
	
	private static int _adult_freq_tag= 8;
	private static int _adult_freq_url=9;
	private static int _adult_freq_domain=10;
	
	int offset= 10000000;
	
	
	static Hashtable<Integer, ArrayList<TagFreq>> url_tags =  new Hashtable<Integer,   ArrayList<TagFreq>> ();

	static Hashtable<Integer, DeliciousNodeSimple> nodes = new Hashtable<Integer, DeliciousNodeSimple> ();
	
	public static void trimByFreq(String path, AgeAggregator ag) {

		FileInput in = new FileInput(path);
		String line = in.readString();
		
		
		// add url ids

		while (line != null) {

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer
					.valueOf(fields[_freq_tag]));
		//	Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer
					.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			//String url_name = fields[_url];

			Integer adult_freq_tag = 0;
			//Integer adult_freq_url = 0;
			//Integer adult_freq_domain = 0;

			if (fields.length > _adult_freq_domain) {
				adult_freq_tag = Integer.valueOf(Integer
						.valueOf(fields[_adult_freq_tag]));

		//		adult_freq_url = Integer.valueOf(Integer.valueOf(fields[_adult_freq_url]));
//
	//			adult_freq_domain = Integer.valueOf(Integer.valueOf(fields[_adult_freq_domain]));

			}

			// create node
			
			if(!ag.isAgeValid(age)){
				
				line=in.readString();
				continue;
			}

			DeliciousNodeSimple tag = null;
			if (nodes.containsKey(id_tag)) {
			
					
					tag = nodes.get(id_tag);
					tag.freq= tag.freq + freq_tag;
					
					nodes.put(id_tag, tag);
		
			

			} else {
				tag = new DeliciousNodeSimple();
				tag.freq =freq_tag;
			
				tag.name=name;
				tag.freq_adult= adult_freq_tag;
				nodes.put(id_tag, tag);


			}

			 ArrayList<TagFreq>h = new  ArrayList<TagFreq>();
			
			if (url_tags.containsKey(id_url)) {

				h = url_tags.get(id_url);
			}

			TagFreq tf= new TagFreq();
			tf.id= id_tag;
			tf.freq= tf.freq+ freq_ut;
			tf.freq_adults= tf.freq_adults+ adult_freq_tag;
			h.add(tf);
			

			url_tags.put(id_url, h);

			line = in.readString();
			fields=null;
		}

		// sum frequencies

		Enumeration<Integer> keys_urls = url_tags.keys();
		Hashtable<Integer, Hashtable<Integer, Integer[]>> cc = new Hashtable<Integer, Hashtable<Integer, Integer[]>>();
		while (keys_urls.hasMoreElements()) {

			int url = keys_urls.nextElement();
			//System.out.println("aggregating " + age);

			ArrayList<TagFreq> list = url_tags.get(url);



			for (int i = 0; i < list.size() - 1; i++) {
				TagFreq a = list.get(i);
				String a_name = nodes.get(a.id).name;
				for (int j = i + 1; j < list.size(); j++) {

					TagFreq b = list.get(j);
					String b_name = nodes.get(b.id).name;

					if (a_name.compareTo(b_name) < 0) {
						Integer freq[] = new Integer[2];
						if (a.freq < b.freq) {
							freq[0] = a.freq;
							freq[1] = a.freq_adults;
						} else {
							freq[0] = b.freq;
							freq[1] = b.freq_adults;

						}

						if (cc.containsKey(a.id)) {


							Hashtable<Integer,Integer[]> tt_b = cc.get(a.id);
							if (tt_b.containsKey(b.id)) {
								Integer[]sum = tt_b.get(b.id);
								sum[0] = sum[0] + freq[0];
								sum[1] = sum[1] + freq[1];

								tt_b.put(b.id, sum);
							} else {

								tt_b.put(b.id, freq);
							}
						} else {
							Hashtable<Integer, Integer[]> tt_b = new Hashtable<Integer, Integer[]>();

							tt_b.put(b.id, freq);

							cc.put(a.id, tt_b);
						}

					} else {
						Integer freq[] = new Integer[2];
						if (a.freq < b.freq) {
							freq[0] = a.freq;
							freq[1] = a.freq_adults;
						} else {
							freq[0] = b.freq;
							freq[1] = b.freq_adults;

						}
						if (cc.containsKey(b.id)) {
							Hashtable<Integer, Integer[]> tt_b = cc
									.get(b.id);
							if (tt_b.containsKey(a.id)) {
								Integer[]sum = tt_b.get(a.id);
								sum[0] = sum[0] + freq[0];
								sum[1] = sum[1] + freq[1];

								tt_b.put(a.id, sum);
							} else {

								tt_b.put(a.id, freq);
							}

							cc.put(b.id, tt_b);
						} else {
							Hashtable<Integer, Integer[]> tt_b = new Hashtable<Integer, Integer[]>();

							tt_b.put(a.id, freq);

							cc.put(b.id, tt_b);
						}

					}

				}

			}

		}


		

	
			Enumeration<Integer> keys_a = cc.keys();
			while (keys_a.hasMoreElements()) {

				Integer a = keys_a.nextElement();
				Hashtable<Integer, Integer[]> c_age_b = cc.get(a);
				Enumeration<Integer> keys_b = c_age_b.keys();
				while (keys_b.hasMoreElements()) {
					Integer b = keys_b.nextElement();

					Integer[] freq = c_age_b.get(b);

					DeliciousNodeSimple aa = nodes.get(a);
					DeliciousNodeSimple bb = nodes.get(b);
				//	System.out.println(aa + "\t"+ a + "\t"+ age + "\t"+ b);
					
					int freq_a = aa.freq;
					int freq_b = bb.freq;

					int freq_a_adult = aa.freq_adult;
					int freq_b_adult = bb.freq_adult;

					System.out.println(a+ "\t" + b + "\t" + aa.name + "\t"
							+ bb.name + "\t" + freq_a + "\t" + freq_b + "\t"
							+ freq[0] + "\t" +  freq_a_adult + "\t"
							+ freq_b_adult + "\t" + freq[1]);

				}

				
				c_age_b.clear();
				c_age_b=null;
				keys_b=null;
			}

		}

	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String path_gold ="/Users/sergioduarte/projects/data/query_suggestions/query_suggestions/query_suggestions.txt";
		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz_youth_mix_simple.txt";
		
		
		AgeAggregator agg= new AgeAggregator(true,true,false,false,false,false,false);
		trimByFreq(path,agg);
		
		
		
	}

	


}


